const express = require('express');
const router = express.Router();
const con = require("../db");
const con2 = require("../db/mysql");
router.get('/', (req, res, next) => {
  const { page, limit } = req.query;
  con.query(`SELECT * FROM role ORDER BY id DESC LIMIT ${limit} OFFSET ${(page - 1) * limit}; SELECT COUNT(*) AS total FROM role`, (err, result) => {
    if (err) {
      console.error("Error executing query:", err);
      res.status(500).json({
        msg: "查询失败",
        code: 500
      });
      return;
    }

    res.json({
      msg: "查询成功",
      code: 200,
      data: result[0],
      total: result[1][0].total
    });
  });
});
router.get('/:id', (req, res, next) => {
  const { id} = req.params;
  con.query(`SELECT * FROM role where id=? ;`,id, (err, result) => {
    if (err) {
      console.error("Error executing query:", err);
      res.status(500).json({
        msg: "查询失败",
        code: 500
      });
      return;
    }

    res.json({
      msg: "查询成功",
      code: 200,
      data: result[0],
      
    });
  });
});

router.post('/', async (req, res, next) => {
  const { body } = req;
  try{const result=await con2.query(`SELECT * FROM role WHERE rolename=?`, body.rolename);
  if(result.length>0){
    res.json({
              msg: "角色名称重复",
              code: 500
            });
  }else{
    const result=await con2.query(`INSERT INTO role (rolename) VALUES (?)`, body.rolename);
    if(result.affectedRows===1){
      res.json({
                    msg: "添加成功",
                    code: 200
                  });
    }else{
      res.json({
        msg: "添加失败",
        code: 500
      });
    }
  }}catch(e){
    throw e;
  }
});

router.delete('/:id', (req, res) => {
  const { id } = req.params;
  con.query(`DELETE FROM role WHERE id=?`, id, (err, result) => {
    if (err) throw err;

    if (result.affectedRows === 1) {
      res.json({
        msg: "删除成功",
        code: 200
      });
    } else {
      res.json({
        msg: "删除失败",
        code: 500
      });
    }
  });
});

router.put('/:id', (req, res) => {
  const { id } = req.params;
  const { state, rolename ,menu} = req.body;

  if (state !== undefined) {
    con.query(`UPDATE role SET state=? WHERE id=?`, [state, id], (err, result) => {
      if (err) throw err;

      if (result.affectedRows === 1) {
        res.json({
          msg: "更新成功",
          code: 200
        });
      } else {
        res.json({
          msg: "更新失败",
          code: 500
        });
      }
    })
  } else if (rolename != undefined) {
    con.query(`SELECT * FROM role WHERE rolename=?`, rolename, (err, result) => {
      if (err) throw err;

      if (result.length) {
        res.json({
          msg: "编辑失败",
          code: 500
        });
      } else {
        con.query(`UPDATE role SET rolename=? WHERE id=?`, [rolename, id], (err, result) => {
          if (err) throw err;

          if (result.affectedRows === 1) {
            res.json({
              msg: "编辑成功",
              code: 200
            });
          }
        })
      };


    })
  }else if (menu != undefined) {
    con.query(`UPDATE role SET menu=? WHERE id=?`, [menu, id], (err, result) => {
      if (err) throw err;

      if (result.affectedRows === 1) {
        res.json({
          msg: "权限修改成功",
          code: 200
        });
      } 
    })
  };
});

module.exports = router;
